<?php 

class User_FriendController extends Zend_Controller_Action
{
	public function addAction()
	{
		$sessAcc = Account_Model_Session::getAccount();

		if ($sessAcc != NULL) {
			$idName = $this->_getParam('entity_idname');
			$user = User_Model_User::getInstanceByIdName($idName);
			if ($user == NULL) {
				Euso_Model_Log::push('user', 'friend.add.failed', 'User not found',
					array('userIdName' => $idName));

				throw new Zend_Controller_Action_Exception('User not found', 500);
			}

			User_Model_RelationshipMap::set($sessAcc->getId(), $user->getId(), 'friend', TRUE);


			if (Kernel::getInstance()->isModuleActive('activity')) {
				Activity_Model_Logger::push('user', $sessAcc->getId(), 'befriend',
					array(
						array('class'=>'user', 'id'=>$user->getId(), 'role'=>'object')
					)
				);
			}


			$this->_redirect($user->getUrl());

		} else {
			$fmsg = uniqid();
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($fmsg);
			$flashMessenger->addMessage('You need to be logged in to request friendship');
			$this->_redirect('/login?fmsg=' . $fmsg .'&return='. urlencode($_SERVER['REQUEST_URI']));
		}
	}
	public function removeAction()
	{
		$sessAcc = Account_Model_Session::getAccount();

		if ($sessAcc != NULL) {
			$idName = $this->_getParam('entity_idname');
			$user = User_Model_User::getInstanceByIdName($idName);
			if ($user == NULL) {
				Euso_Model_Log::push('user', 'friend.remove.failed', 'User not found',
					array('userIdName' => $idName));

				throw new Zend_Controller_Action_Exception('User not found', 500);
			}

			User_Model_RelationshipMap::clear($sessAcc->getId(), $user->getId(), 'friend');

			$this->_redirect($user->getUrl());

		} else {
			$fmsg = uniqid();
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($fmsg);
			$flashMessenger->addMessage('You need to be logged in to remove friendship');
			$this->_redirect('/login?fmsg=' . $fmsg .'&return='. urlencode($_SERVER['REQUEST_URI']));
		}
	}
}